//
//  CustomTabBarItemView.m
//  tabbardemo
//
//  Created by 虞嘉伟 on 2025/2/24.
//


#import "CustomTabBarItemView.h"
#import <YYKit.h>
#import <Masonry.h>

@interface CustomTabBarItemView ()
@property (nonatomic, strong) UIImage *normal;
@property (nonatomic, strong) UIImage *select;
@end
@implementation CustomTabBarItemView

- (instancetype)initWithTitle:(nullable NSString *)title image:(nullable UIImage *)image selectedImage:(nullable UIImage *)selectedImage {
    if (self = [super init]) {
        self.normal = image;
        self.select = selectedImage;
        
        // 背景
        _bgView = [UIView new];
        [self addSubview:_bgView];
        [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            make.height.mas_equalTo(38);
        }];
        
        // 图标
        _iconImageView = [UIImageView new];
        _iconImageView.image = image;
        //_iconImageView.frame = CGRectMake((frame.size.width - 30) / 2, 5, 30, 30);
        _iconImageView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:_iconImageView];
        [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bgView).inset(10);
            make.centerY.equalTo(self.bgView);
            make.width.height.mas_equalTo(20);
        }];
        
        // 标题
        _titleLabel = [UILabel new];
        _titleLabel.text = title;
        _titleLabel.font = [UIFont systemFontOfSize:10];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textColor = [UIColor grayColor];
        [self addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_iconImageView.mas_right).offset(10);
            make.right.equalTo(self.bgView).inset(10);
            make.centerY.equalTo(_iconImageView);
        }];
        
        // 默认未选中
        _isSelected = NO;
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.bgView.layer.cornerRadius = CGRectGetHeight(self.bgView.bounds)*0.5;
}

- (void)setIsSelected:(BOOL)isSelected {
    _isSelected = isSelected;
    if (isSelected) {
        _iconImageView.image = _select;
        _titleLabel.textColor = UIColorHex(#1B1B1B);
        self.bgView.backgroundColor = UIColorHex(#FDC5E0); //粉色
    } else {
        _iconImageView.image = _normal;
        _titleLabel.textColor = UIColor.whiteColor;
        self.bgView.backgroundColor = UIColorHex(#FFFFFF80); //半透明白色
    }
}

@end
